package com.xingyue.loginqueryarticle.controllers;

import com.xingyue.loginqueryarticle.Dao.QueryDao;
import com.xingyue.loginqueryarticle.entity.User;
import com.xingyue.loginqueryarticle.service.QueryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * Created by hdwx on 2016/11/18.
 */
@WebServlet(urlPatterns = "/Login.do")
public class LoginController extends BaseCtroller{

   private QueryService service=new QueryService();

    @Override
    public void todo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取 属性
        String name=req.getParameter("name");
 String password=req.getParameter("password");
        String isRem = req.getParameter("isRem");
User user1 =new User();
   //赋值给User类
user1.setName(name);
user1.setPassword(password);

       boolean ba =  service.loginlost(name,password);

        if(ba){
            Cookie cookie = new Cookie("name", URLEncoder.encode(name,"utf-8"));
            cookie.setMaxAge(10);//设置失效时间10秒,负数表示永远不失效
            Cookie cookie1 = new Cookie("password", password);


            System.out.println(name);
            resp.addCookie(cookie);
            resp.addCookie(cookie1);
            resp.sendRedirect("Page.jsp");
        }else{
            resp.getWriter().append("<h1>登录失败,<a href='login.html'>重新登录</a></h1>");
        }
    }
}
